package com.pucong.gitee.database.backup.database.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pucong.gitee.database.backup.common.entity.BackupDatabase;
import com.pucong.gitee.database.backup.common.selfenum.TreeType;
import com.pucong.gitee.database.backup.common.vo.DbBackTree;
import com.pucong.gitee.database.backup.database.service.BackupDatabaseService;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pucong
 * @company www.louddt.com
 * @create 2020-06-23
 */
@RestController
@RequestMapping("/backup/database")
@Api(value = "数据库信息表-API ", tags = "1. 数据库信息表 API")
public class BackupDatabaseController {

    @Autowired
    private BackupDatabaseService backupDatabaseService;

    @GetMapping("/getDatabaseListByPage/{pageNum}/{pageSize}")
    @ApiOperation(value = "101分页查询", notes = "分页条件查询")
    public Res<IPage<BackupDatabase>> getDatabaseListByPage(@ApiParam(value = "页数",example = "1")@PathVariable("pageNum") int pageNum, @ApiParam(value = "一页大小",example = "10")@PathVariable("pageSize") int pageSize, BackupDatabase backupDatabase){
        IPage<BackupDatabase> backupDatabaseIPage = backupDatabaseService.getDatabaseListByPage(pageNum,pageSize,backupDatabase);
        return Res.ok(backupDatabaseIPage);
    }

    @GetMapping("/getTreeListByPid/{id}/{treeType}")
    @ApiOperation(value = "104根据上级 查询下级树数据")
    public Res<List<DbBackTree>> getTreeList(@ApiParam(value = "父id")@PathVariable("id") String id,@ApiParam(value = "父id")@PathVariable("treeType") TreeType treeType){
        List<DbBackTree> databaseTreeList = backupDatabaseService.getTreeList(id,treeType);
        return Res.ok(databaseTreeList);
    }

    @GetMapping("/getBaseInfoById/{id}")
    @ApiOperation(value = "102根据主键查询", notes = "根据主键查询")
    public Res<BackupDatabase> getBaseInfoById(@ApiParam("主键")@PathVariable("id") String id){
        BackupDatabase backupDatabase = backupDatabaseService.getBaseInfoById(id);
        return Res.ok(backupDatabase);
    }

    @PostMapping("/saveBaseInfo")
    @ApiOperation(value = "103新增和修改数据库信息")
    public Res<Integer> addBaseInfo(@Validated @RequestBody BackupDatabase backupDatabase){
        int i = backupDatabaseService.saveBaseInfo(backupDatabase);
        return Res.ok(i);
    }
}
